//
//  SecondClassifyViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/5/22.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "SecondClassifyViewController.h"
#import "SecondClassModel.h"
#import "UserInfoModel.h"
#import "ClassifyActivityController.h"

@interface SecondClassifyViewController ()<UITableViewDelegate, UITableViewDataSource>

// UI
@property (nonatomic,retain) UITableView *tableView;

// 数据
@property (nonatomic,retain) NSMutableArray *dataArray;

@end

@implementation SecondClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取分类数据
    [self getClassify];
    
    // 初始化tableView
    [self setupTableView];
    
    self.title = @"活动类别";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"alert"] style:UIBarButtonItemStylePlain target:self action:@selector(showAlert)];
    self.navigationItem.rightBarButtonItem = item;
}

#pragma mark - UI设置
- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

-(void)showAlert {
    SecondClassModel *model = self.dataArray.firstObject;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:model.require
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:done];
    
    [self presentViewController:alert animated:true completion:nil];
}

#pragma mark - 网络请求
- (void)getClassify {
    [SVProgressHUD show];
    self.dataArray = [[NSMutableArray alloc] init];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getActivityItems"
                                        parameters:@{@"sid": [UserInfoModel shareInstance].stu_id, @"tid": @(self.tid)}
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               for (NSDictionary *item in respondDictionary[@"items"]) {
                                                   SecondClassModel *model = [[SecondClassModel alloc] initWithDictionary:item];
                                                   [self.dataArray addObject:model];
                                               }
                                               [self.tableView reloadData];
                                           } failure:^(NSError *error) {
                                               [SVProgressHUD dismiss];
                                               
                                               [SVProgressHUD showfailed];
                                           }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELLID"];
    SecondClassModel *model = self.dataArray[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"总次数:%d     总学分:%d",model.count, model.score];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    ClassifyActivityController *controller = [[ClassifyActivityController alloc] init];
    SecondClassModel *model = self.dataArray[indexPath.row];
    controller.iid = model.iid;
    [self.navigationController pushViewController:controller animated:true];
}


@end
